package org.yjw.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author 31898
 * @date 2021.11.03
 * 切面类 (可以写增强代码和切入点，）
 * 通过注解的形式实现SpringAOP
 * @Aspect 注解标注该类是一个切面类
 * 需要在xml中配置 <aop:aspectj-autoproxy/> 以对注解的支持
 */
@Aspect
@Component
public class AnnotationAspectTest {

    /**
     * @param jp
     * @Before 表示前置增强
     */
//    @Before(value = "execution(* *..*.*Impl.*(..))")
    @Before(value = "AnnotationAspectTest.pointcat()")
    public void before(JoinPoint jp) {
        System.out.println("前置增强");
    }

    /**
     * 后置增强
     *
     * @param jp
     */
//    @AfterReturning(value = "execution(* *..*.*Impl.*(..))")
    @AfterReturning(value = "AnnotationAspectTest.pointcat()")
    public void afterReturning(JoinPoint jp) {
        System.out.println("后置增强");
    }

    /**
     * 异常抛出增强
     *
     * @param jp
     */
//    @AfterThrowing(value = "execution(* *..*.*Impl.*(..))")
    @AfterThrowing(value = "AnnotationAspectTest.pointcat()")
    public void afterThrowing(JoinPoint jp) {
        System.out.println("异常抛出增强");
    }

    /**
     * 最终增强
     *
     * @param jp
     */
//    @After(value = "execution(* *..*.*Impl.*(..))")
    @After(value = "AnnotationAspectTest.pointcat()")
    public void after(JoinPoint jp) {
        System.out.println("最终增强");
    }

    /**
     * @param jp
     * @return
     * @Around 环绕增强
     */
//    @Around(value = "execution(* *..*.*Impl.findByid(..))")
    public void around(ProceedingJoinPoint jp) {
        System.out.println("环绕增强");
    }

    /**
     * 定义通用的切入点
     */
    @Pointcut(value = "execution(* *..*.*Impl.*(..))")
    public void pointcat() {
    }


}
